package com.finapps.stopspend.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.finapps.stopspend.R;
import com.finapps.stopspend.entidades.ObjetivoEntity;
import com.finapps.stopspend.entidades.SituacioObjectiu.ColorSituacio;
import com.finapps.stopspend.util.CalendarUtils;

public class ObjetivosAdapter extends ArrayAdapter<ObjetivoEntity> {

	private Activity ctx;
	
	public ObjetivosAdapter(Activity context, int txt, List<ObjetivoEntity> objects) {
		super(context, txt, objects);
		this.ctx = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vH;


		if (convertView == null)
		{
			LayoutInflater layout = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = layout.inflate(R.layout.row_objeto, null);
			
			vH = new ViewHolder();

			vH.txtTitulo = (TextView) convertView.findViewById(R.id.txtTitolRow);
			vH.color = (View) convertView.findViewById(R.id.colorRow);
			vH.txtDescript = (TextView) convertView.findViewById(R.id.txtDescriptRow);
			vH.txtData = (TextView) convertView.findViewById(R.id.txtDataRow);
			
			vH.txtPrecio = (TextView) convertView.findViewById(R.id.txtPrecioRow);
			vH.txtAcumulado = (TextView) convertView.findViewById(R.id.txtAcumuladoRow);
			
			vH.imagen = (ImageView) convertView.findViewById(R.id.imgRow);
			convertView.setTag(vH);
		} else {
			vH = (ViewHolder) convertView.getTag();
		}
		
		ObjetivoEntity obj = this.getItem(position);
		vH.txtTitulo.setText(obj.getTitulo());
		
		ColorSituacio valor = obj.getColorSituacio();
		if (valor == null) valor = ColorSituacio.VERD;
		switch (valor)
		{
		case VERD:
			vH.color.setBackgroundColor(ctx.getResources().getColor(R.color.iWish));
			break;
		case GROC:
		case TARONJA:
			vH.color.setBackgroundColor(ctx.getResources().getColor(R.color.soda));
			break;
		case VERMELL:
			vH.color.setBackgroundColor(ctx.getResources().getColor(R.color.flame));
			break;
		}
		if (obj.getDescripcion().length() > 0)
			vH.txtDescript.setText(obj.getDescripcion());
		else
			vH.txtDescript.setVisibility(View.GONE);
		
		String precio = String.format(ctx.getResources().getString(R.string.precioD), obj.getPrecio().toString(), null);
		vH.txtPrecio.setText(precio);
		String acumulado = String.format(ctx.getResources().getString(R.string.acumuladoD), obj.getFalta().toString(), null);
		vH.txtAcumulado.setText(acumulado);
		
		vH.imagen.setImageDrawable(ctx.getResources().getDrawable(R.drawable.puntosestrella));
		
		CalendarUtils cal = new CalendarUtils();
		
		vH.txtData.setText(cal.calendar2Data2(obj.getData()));
		
		return convertView;
		
		
	}
	
	static class ViewHolder
	{
		TextView txtTitulo;
		View color;

		TextView txtDescript;
		TextView txtData;
		TextView txtPrecio;
		TextView txtAcumulado;
		
		ImageView imagen;
	}
}
